package com.app.twth.ui.b2cpick.category;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.twth.databinding.ItemB2cpickCategoryLocationLpnListBinding;
import com.app.twth.interfaces.MyStringOnClickListener;
import com.app.twth.ui.b2cpick.B2CPickDetails;

import java.util.ArrayList;
import java.util.List;


public class B2CPickCategoryLocationLPNAdapter extends RecyclerView.Adapter<B2CPickCategoryLocationLPNAdapter.MyViewHolder> {
    private Context mContext;
    private LayoutInflater mInflater;
    private MyStringOnClickListener lpnListener;
    private List<B2CPickDetails.LocationLpnDTO> data = new ArrayList<>();

    public B2CPickCategoryLocationLPNAdapter(Context mContext, List<B2CPickDetails.LocationLpnDTO> data, MyStringOnClickListener lpnListener) {
        this.mContext = mContext;
        this.data = data;
        this.mInflater = LayoutInflater.from(mContext);
        this.lpnListener = lpnListener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(ItemB2cpickCategoryLocationLpnListBinding.inflate(mInflater, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ItemB2cpickCategoryLocationLpnListBinding itemBinding;

        public MyViewHolder(ItemB2cpickCategoryLocationLpnListBinding itemBinding) {
            super(itemBinding.getRoot());
            this.itemBinding = itemBinding;
        }

        public void setData(int position) {
            itemView.setTag(position);
            B2CPickDetails.LocationLpnDTO locationsDTO = data.get(position);
            itemBinding.tvItem.setText(locationsDTO.getLocationCode() + "");
            List<B2CPickDetails.IbLpnListDTO> lpns = locationsDTO.getIbLpnList();

            B2CPickCategoryLpnAdapter lpnAdapter = new B2CPickCategoryLpnAdapter(mContext, lpns, lpnListener);
            LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            itemBinding.recyclerviewLocation.setLayoutManager(layoutManager);
            itemBinding.recyclerviewLocation.setAdapter(lpnAdapter);
        }
    }

}
